package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.cj.pdos.entity.UserHang;
import pers.cj.pdos.entity.UserHangGoods;
import pers.cj.pdos.entity.UserHangSn;
import pers.cj.pdos.mapper.UserHangGoodsMapper;
import pers.cj.pdos.mapper.UserHangMapper;
import pers.cj.pdos.mapper.UserHangSnMapper;
import pers.cj.pdos.service.IUserHangService;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.UserHangVo;

import java.util.List;

/**
 * <p>
 * 用户的在零售收银的挂单，每个用户最多只能挂一单内容 服务实现类
 * </p>
 *
 * @author chenj
 * @since 2020-03-19
 */
@Service
public class UserHangServiceImpl extends ServiceImpl<UserHangMapper, UserHang> implements IUserHangService {
    @Autowired
    UserHangMapper userHangMapper;
    @Autowired
    UserHangGoodsMapper userHangGoodsMapper;
    @Autowired
    UserHangSnMapper userHangSnMapper;

    @Override
    public void insert(UserHang userHang, List<UserHangGoods> userHangGoodsList, List<UserHangSn> userHangSnList) {
        userHangSnMapper.delete(new QueryWrapper<UserHangSn>().eq("user_id", userHang.getUserId()));
        userHangGoodsMapper.delete(new QueryWrapper<UserHangGoods>().eq("user_id", userHang.getUserId()));
        userHangMapper.delete(new QueryWrapper<UserHang>().eq("user_id", userHang.getUserId()));

        userHangMapper.insert(userHang);
        userHangGoodsMapper.batchInsert(userHangGoodsList);
        if (!Utils.isEmpty(userHangSnList)){
            userHangSnMapper.batchInsert(userHangSnList);
        }
    }

    @Override
    public Boolean checkHangExistByUserId(String userId) {
        return userHangMapper.selectCount(new QueryWrapper<UserHang>().eq("user_id",userId)) > 0;
    }

    @Override
    public UserHangVo getVoByUserId(String userId,String houseId) {
        return userHangMapper.getVoByUserId(userId,houseId);
    }

    @Override
    public void deleteByUserId(String userId) {
        userHangSnMapper.delete(new QueryWrapper<UserHangSn>().eq("user_id",userId));
        userHangGoodsMapper.delete(new QueryWrapper<UserHangGoods>().eq("user_id", userId));
        userHangMapper.delete(new QueryWrapper<UserHang>().eq("user_id", userId));
    }

    @Override
    public boolean checkHangExistByStoreId(String storeId) {
        return userHangMapper.checkHangExistByStoreId(storeId) > 0;
    }
}
